package org.springblade.modules.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.models.auth.In;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.UserBusinessSet;
import org.springblade.modules.system.enums.UserBusinessSetKeyEnum;
import org.springblade.modules.system.enums.UserBusinessSetValueEnum;
import org.springblade.modules.system.service.UserBusinessSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;

/**
 * @Date 11:26 2023/2/21
 * @Description {
 *   人员业务开关
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/blade-user/business/set")
public class UserBusinessSetController {

	@Autowired
	private UserBusinessSetService userBusinessSetService;

	/**
	 * @Author lsay
	 * @Description {
	 *   获取人员业务开关值
	 * }
	 * @Date 11:28 2023/2/21
	 * @Param
	 * @return
	 **/
	@GetMapping()
	public R<UserBusinessSet> getUserBusinessSet(Long userId){
		UserBusinessSet userBusinessSet = userBusinessSetService.getOne(Wrappers.<UserBusinessSet>lambdaQuery().eq(UserBusinessSet::getUserId,userId).eq(UserBusinessSet::getConfigKey, UserBusinessSetKeyEnum.CHECK_CONTENT_ALL.getType()));
		if(ObjectUtil.isEmpty(userBusinessSet)){
			userBusinessSet = new UserBusinessSet();
			userBusinessSet.setUserId(userId);
			userBusinessSet.setConfigValue(UserBusinessSetValueEnum.CHECK_CONTENT_ALL_OFF.getValue());
			userBusinessSet.setConfigKey(UserBusinessSetKeyEnum.CHECK_CONTENT_ALL.getType());
			userBusinessSet.setPictureDisable(UserBusinessSetValueEnum.CHECK_CONTENT_ALL_ON.getValue());
		}
		return R.data(userBusinessSet);
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   更新人员业务开关值
	 * }
	 * @Date 11:31 2023/2/21
	 * @Param
	 * @return
	 **/
	@PostMapping()
	public R<Boolean> updateUserBusinessSet(@RequestBody UserBusinessSet entity){
		UserBusinessSet userBusinessSet = userBusinessSetService.getOne(Wrappers.<UserBusinessSet>lambdaQuery().eq(UserBusinessSet::getUserId,entity.getUserId()).eq(UserBusinessSet::getConfigKey, UserBusinessSetKeyEnum.CHECK_CONTENT_ALL.getType()));
		if(ObjectUtil.isEmpty(userBusinessSet)){
			userBusinessSet = new UserBusinessSet();
			userBusinessSet.setUserId(entity.getUserId());
			userBusinessSet.setConfigKey(UserBusinessSetKeyEnum.CHECK_CONTENT_ALL.getType());
		}
		userBusinessSet.setConfigValue(entity.getConfigValue());
		userBusinessSet.setPictureDisable(entity.getPictureDisable());
		return R.status(userBusinessSetService.saveOrUpdate(userBusinessSet));
	}

}
